//
// Description: 7. 整数反转
// Created by Loading on 2021/5/3.
//

#include <bits/stdc++.h>

using namespace std;

int reverse(int x) {
    /* 题目要求不允许使用64位有符号或无符号整数 */
    int res = 0;
    while (x != 0) {
        //由于res后边还会 * 10 迭代，故如果此时res已经超过int值的极限 / 10，则结果必溢出
        if (res > INT_MAX / 10 || res < INT_MIN / 10) {
            return 0;
        }
        res = res * 10 + x % 10;
        x /= 10;
    }

    return res;
}

int main() {
    int x1 = 123;
    int x2 = -123;
    int x3 = 0;
    int x4 = 2101234567;

    cout << reverse(x1) << endl;
    cout << reverse(x2) << endl;
    cout << reverse(x3) << endl;
    cout << reverse(x4) << endl;
}